﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medical.Business;
using Medical.Entity;
using System.Data;

/// <summary>
/// 广告栏列表
/// </summary>
public partial class webadmin_BannerList : AdminPage
{
    /// <summary>
    /// 广告类型
    /// </summary>
    protected AdverTypeEnum eType;

    protected void Page_Load(object sender, EventArgs e)
    {
        eType = (AdverTypeEnum)Convert.ToInt32(Common.GetQueryValue("type", 0));
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Adver;
    }

    /// <summary>
    /// 数据初始化
    /// </summary>
    protected void InitData()
    {
        BisBanner bisBanner = new BisBanner();
        //列表绑定
        DataTable dt = null;
        if (eType == AdverTypeEnum.Banner)
        {
            dt = bisBanner.GetListByOrderNum(eType);
            dt.Columns.Add("SortName", Type.GetType("System.String"));
        }
        else if (eType == AdverTypeEnum.Disease || eType == AdverTypeEnum.FontDisease)
        {
            dt = bisBanner.GetListBySort(eType);
            
        }else
        {
            dt = bisBanner.GetList(eType);
            dt.Columns.Add("SortName", Type.GetType("System.String"));
        }
        if (dt!=null&&dt.Rows.Count>0)
        {
            noData.Style["display"] = "none";
            
        }
        else
        {
            noData.Style["display"] = "block";
        }
        rptBanner.DataSource = dt;
        rptBanner.DataBind();
        dt.Dispose();
    }

    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptBanner_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtSort = (DataTable)((Repeater)sender).DataSource;
        ImageButton btnUp = e.Item.FindControl("btnUp") as ImageButton;
        ImageButton btnDown = e.Item.FindControl("btnDown") as ImageButton;
        int nItemID = e.Item.ItemIndex;
        if (nItemID == 0)
        {
            //禁用第一个上移按钮
            btnUp.Enabled = false;
            btnUp.ImageUrl = "images/noup.gif";
            btnUp.CommandArgument = "";

        }
        if (nItemID == (dtSort.Rows.Count - 1))
        {
            //禁用最后一个下移按键
            btnDown.Enabled = false;
            btnDown.ImageUrl = "images/nodown.gif";
            btnDown.CommandArgument = "";
        }
    }

    /// <summary>
    /// 分类列表绑定
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptBanner_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        BisBanner bisBanner = new BisBanner();
        bool result = false;
        if (e.CommandName == "OrderUp" || e.CommandName == "OrderDown")
        {
            string[] itemlist = e.CommandArgument.ToString().Split(',');
            int nID = Convert.ToInt32(itemlist[0]);
            int nOrder = Convert.ToInt32(itemlist[1]);
            int nOtherID = 0;
            int nOtherOrder = 0;

            //上移下移操作
            if (e.CommandName == "OrderDown")
            {
                itemlist = ((HiddenField)((Repeater)sender).Items[e.Item.ItemIndex + 1].FindControl("hfID")).Value.ToString().Split(',');
                nOtherID = Convert.ToInt32(itemlist[0]);
                nOtherOrder = Convert.ToInt32(itemlist[1]);
            }
            else
            {
                itemlist = ((HiddenField)((Repeater)sender).Items[e.Item.ItemIndex - 1].FindControl("hfID")).Value.ToString().Split(',');
                nOtherID = Convert.ToInt32(itemlist[0]);
                nOtherOrder = Convert.ToInt32(itemlist[1]);
            }
            result = bisBanner.ChangeOrder(nID, nOtherOrder, nOtherID, nOrder);
            if (!result)
                Until.Alert("操作失败！");
        }
        else if (e.CommandName == "Delete")
        {
            //删除信息
            string[] itemlist = e.CommandArgument.ToString().Split(',');
            int nID = Convert.ToInt32(itemlist[0]);
            result = bisBanner.Delete(nID);
            if (result)
                Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + itemlist[1]));
            Until.Alert(result ? "删除成功！" : "删除失败！");
        }

        //重新加载
        if (result)
        {
            InitData();
        }
    }

    /// <summary>
    /// 刷新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        InitData();
    }

    /// <summary>
    /// 是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckDisplay()
    {
        switch (eType)
        {
            case AdverTypeEnum.Environment:
            case AdverTypeEnum.Banner:
            case AdverTypeEnum.Honor:
            
                return "display:none";
        }
        return "";
    }

    /// <summary>
    /// 是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckDisplay2()
    {
        switch (eType)
        {
            case AdverTypeEnum.Banner:
                return "display:none";
        }
        return "";
    }

    /// <summary>
    /// 是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckDisplay1()
    {
        switch (eType)
        {
            case AdverTypeEnum.Banner:
            case AdverTypeEnum.Disease:
            case AdverTypeEnum.FontDisease:
            case AdverTypeEnum.Health:
            case AdverTypeEnum.Health1:
                return "display:none";
        }
        return "";
    }

    /// <summary>
    /// 是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckDisplay3()
    {
        switch (eType)
        {
            case AdverTypeEnum.Disease:
            case AdverTypeEnum.FontDisease:
                return "";
        }
        return "display:none";
    }

    /// <summary>
    /// 是否显示
    /// </summary>
    /// <returns></returns>
    protected string CheckDisplay4()
    {
        switch (eType)
        {
            case AdverTypeEnum.FontDisease:
            case AdverTypeEnum.Health:
            case AdverTypeEnum.Health1:
                return "display:none;";
        }
        return "";
    }
}